package com.zeng.vuerestful.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zeng.nicecommon.util.MapBuilder;
import com.zeng.nicecommon.util.ResponseUtil;
import com.zeng.vueserve.coreapi.vo.UserRoleVo;
import com.zeng.vueserve.coreapi.service.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RequestMapping("/userRole")
@RestController
public class UserRoleController {

    @Autowired
    UserRoleService userRoleService;

    @RequestMapping("/list")
    public Object list(Page page, UserRoleVo userRoleVo) {
        List<UserRoleVo> page1 = this.userRoleService.findPage(page, userRoleVo);

        ArrayList<Map<String,Object>> objects = new ArrayList<>();
        page1.forEach(e->{
            Map<String, Object> build = MapBuilder.newBuilder()
                    .put("code", e.getCode())
                    .put("updateDate", e.getUpdateDate())
                    .put("insertDate", e.getInsertDate())
                    .put("id", e.getId())
                    .put("userCode", e.getUserCode())
                    .put("roleCode", e.getRoleCode())
                    .build();
            objects.add(build);

        });
        return ResponseUtil.success("操作成功")
                .addData("userRole", objects)
                .addData("page", page);

    }

    @RequestMapping("/detail")
        public Object detail(String code) {

        return ResponseUtil.success("操作成功").addData("userRole", userRoleService.getOneByCode(code));

    }

    @RequestMapping("/add")
    public Object add(UserRoleVo userRoleVo) {

        return ResponseUtil.success("操作成功").addData("userRole", userRoleService.addOne(userRoleVo));

    }

    @RequestMapping("/update")
    public Object update(UserRoleVo userRoleVo) {
        Integer update = this.userRoleService.update(userRoleVo);

        return ResponseUtil.success("操作成功")
                .addData("updateCount", update);
    }

    @RequestMapping("/delete")
    public Object delete(String code) {
        Integer delete = this.userRoleService.delete(code);
        return ResponseUtil.success("操作成功").addData("deleteCount", delete);
    }
    @RequestMapping("/batchDelete")
    public Object batchDelete(List<String> codes) {
        Integer integer = this.userRoleService.batchDelete(codes);

        return ResponseUtil.success("操作成功").addData("deleteCount", integer);
    }
}

